/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package UserPackage;

import DBConnection.*;
import java.sql.*;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
/**
 *
 * @author RES
 */
public class cardManager {
    
    public boolean addCC(card newCard)throws ParseException{
          boolean result=false;

        try
        {
              int i = 1;
            //Opens DB Connection
            ConnectionFactory myFactory = ConnectionFactory.getFactory();
            Connection conn = myFactory.getConnection();
            String dates= newCard.getCardExpYr()+"-"+newCard.getCardExpMon()+"-"+newCard.getCardExpDay();
            java.util.Date d;
            DateFormat formatter = new SimpleDateFormat("yyyy-mm-dd");
            d = (java.util.Date)formatter.parse(dates);
            java.sql.Date ccexpdate = new java.sql.Date(d.getTime());

            PreparedStatement pst = conn.prepareStatement("INSERT INTO card(username, creditCardName, creditCardNum, creditCardHash, creditCardType, creditCardExpDate) VALUES(?,?,?,?,?,?)"); 
        
            pst.setString(i++, newCard.getUsername());
            pst.setString(i++, newCard.getCardName());
            pst.setString(i++, newCard.getCardNum());
            pst.setString(i++, newCard.getCardHash());
            pst.setString(i++, newCard.getCardType());
            pst.setDate(i++, ccexpdate);
            pst.executeUpdate();
            //close SQL connection
            conn.close();
            
            result= true;
            

        }
        catch(SQLException ex)
        { ex.printStackTrace();}
    
        return result;
       
    
    }
    
    
}
